package com.cmy.service.user;

import com.cmy.dao.BaseDao;
import com.cmy.dao.user.UserDao;
import com.cmy.dao.user.UserDaoImp1;
import com.cmy.pojo.Role;
import com.cmy.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImp1 implements UserService{
    //业务层都会调用dao层所以需要引入dao层
    private UserDao userDao;
    public UserServiceImp1(){
        userDao = new UserDaoImp1();
    }

    //登录的业务层逻辑,返回登录用户
    public User login(String userCode, String password) {
        User user = new User();
        Connection conn = BaseDao.getConnection();
        //通过业务层,调用具体的数据库操作
        User loginUser = userDao.getLoginUser(conn,userCode,password);
        //关闭资源
        BaseDao.release(conn,null,null);
        return loginUser;
    }

    //修改密码的业务层逻辑
    public boolean updatePwd(int id, String password) {
        Connection conn = null;
        boolean flag = false;
        //修改密码
        try {
            conn = BaseDao.getConnection();
            if (userDao.updatePwd(conn,id,password)>0){
                flag=true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.release(conn,null,null);
        }
        return flag;

    }

    //获取用户数量
    public int getUserCount(String userName, int userRole) {
        Connection connection =null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getCount(connection, userName, userRole);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.release(connection,null,null);
        }
        return count;
    }

    //实现通过用户的姓名,角色以及分页信息返回用户列表 业务层逻辑
    public List<User> getUserList(String queUserName, int queUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        connection = BaseDao.getConnection();
        try {
            userList = userDao.getUserList(connection,queUserName,queUserRole,currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            BaseDao.release(connection,null,null);
        }

        return userList;
    }
    //添加用户

    public boolean addUser(User user) {
        Connection connection = null;
        boolean b = false;

        try {
             connection = BaseDao.getConnection();
             //开启jdbc事务
             connection.setAutoCommit(false);
             b = userDao.addUser(connection, user);
             connection.commit();
             if (b){
                 System.out.println("add sucess");
             }else {
                 System.out.println("add failed");
             }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("rollback-----------");
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }finally {
            BaseDao.release(connection,null,null);
        }
        return b;
    }

    public boolean userCodeExist(String userCode) throws SQLException {
        Connection connection = null;
        boolean flag = false;
        connection = BaseDao.getConnection();
        if (userDao.userCodeExist(connection,userCode)){
            flag = true;
        }else {
            flag = false;
        }
        return flag;
    }

//        @Test
//    public void test(){
//            try {
//                System.out.println(userCodeExist("void"));
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
}

    